﻿using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Web;
using ServiceModelEx;

namespace Catalyst.ServiceModel
{
	public class CloudChannelFactory<T> where T : class
	{
		public static T CreateChannel(Binding binding = null, EndpointAddress endpointAddress = null, Uri scope = null)
		{
			if (HttpContext.Current != null)
			{
				if (binding == null)
				{
					binding = new NetTcpBinding(SecurityMode.Transport, true);
				}

				if (endpointAddress == null)
				{
					endpointAddress = DiscoveryHelper.DiscoverAddress<T>();
				}
			}

			if (binding != null && endpointAddress != null)
			{
				return ChannelFactory<T>.CreateChannel(binding, endpointAddress);
			}
			else
			{
				return DiscoveryFactory.CreateChannel<T>(scope);
			}
		}
	}
}
